// pages/community/post/post.js
import util from '../../../utils/util.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '', // 当前账号id
    post: null, // 当前的帖子
    tempContent:'',
    comments: null, // 所有的评论
    content: '', //发表评论内容
    islogin: false,
    isedit:false, // 显示编辑框
    InputBottom: 0
  },
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    var post, comments, openid // 暂存帖子 和 当前的openid
    wx.cloud.callFunction({
      name: 'getOpenid'  // 获取当前的openid
    }).then(res => {
      openid = res.result.openid
      this.getPost(options.postid).then(res => { // 获取post
        post = res.result.list[0]
        this.getAllComments(options.postid, 0).then(res => {  // 获取comments
          comments = res.result.list
          this.setData({    // 刷新数据到界面
            openid: openid,
            post: post,
            comments: comments,
            islogin: app.globalData.islogin
          })
          wx.hideLoading()
          this.data.post.viewnumber++; //浏览量+ 1 
          wx.cloud.callFunction({ // 更新帖子表中的浏览量
            name: 'updatePost',
            data: {
              post: this.data.post
            }
          })
        })
      })
    })
  },
  getData(e) { //获取评论的内容
    this.setData({
      content: e.detail.value
    })
  },
  sendComment() {  // 评论添加到数据库
    if (app.globalData.islogin == false) {
      wx.showToast({
        icon: 'none',
        title: '请先登陆哦',
      })
    } else if (this.data.content == '') { // 评论为空
      wx.showToast({
        icon: 'none',
        title: '评论不能为空',
      })
    } else {
      var DATE = util.formatDate(new Date)
      wx.cloud.database().collection('comments').add({ // 将评论内容添加到comments表中
        data: {
          comment: this.data.content,
          postid: this.data.post._id,
          time: DATE,
          createTime: Date.parse(new Date())
        }
      }).then(res => {
        this.getAllComments(this.data.post._id, 0)  //重新获取评论
          .then(res => {
            var comments = res.result.list
            this.data.post.commentnumber = comments.length
            wx.cloud.callFunction({    // 修改评论的条数
              name: 'updatePost',
              data: {
                post: this.data.post
              }
            }).then(res => {
              this.setData({
                content: '',
                comments: comments,
                post: this.data.post
              })
              wx.showToast({
                title: '发表成功',
              })
            })
          })
      })
    }
  },
  confirm() {   // 确认是否删除帖子
    wx.showModal({
      title: '提示',
      content: '确定要删除吗?',
      showCancel: true,
      success: res => {
        if (res.confirm) {
          this.deletePost()
        }
      }
    })
  },
  editPost(){  // 显示编辑框
    this.setData({
      isedit: true
    })
  },
  getInput(e){ // 获得修改后的内容
    this.setData({
      tempContent: e.detail.value
    })
  },
  update(){  //提交编辑帖子
    this.setData({
      isedit: false,
      [`post.content`]: this.data.tempContent
    })
    wx.cloud.callFunction({ // 更新帖子表中的浏览量
      name: 'updatePost',
      data: {
        post: this.data.post
      }
    }).then(res=>{
      wx.showToast({
        title: '修改成功',
      })
    })
  },
  cancel(){  // 取消编辑帖子
    this.setData({
      isedit: false
    })
  },
  deletePost() {
    wx.cloud.database().collection('posts').doc(this.data.post._id).remove() // 删除帖子
      .then(res => {
        wx.cloud.callFunction({ // 根据postid把帖子里的全部评论删除
          name: 'deleteComments',
          data: {
            postid: this.data.post._id,
            type: 'postid'
          }
        })
        wx.showToast({
          title: '删除成功',
        })
        wx.reLaunch({
          url: '../../community/community',
        })
      })
  },
  deleteComment(e) {
    wx.showLoading({
      title: '正在删除'
    })
    wx.cloud.callFunction({
      name: 'deleteComments',  // 根据commentid 把评论删除
      data: {
        commentid: e.currentTarget.dataset.commentid,
        type: 'commentid'
      }
    }).then(res => {
      this.data.post.commentnumber--  //修改评论数量
      wx.cloud.callFunction({
        name: 'updatePost',
        data: {
          post: this.data.post
        }
      }).then(res => {
        // 重新获取数据
        this.getPost(this.data.post._id).then(res => {
          var post = res.result.list[0]
          this.getAllComments(this.data.post._id, 0).then(res => {
            this.setData({
              post: post,
              comments: res.result.list
            })
            wx.hideLoading()
            wx.showToast({
              title: '删除成功',
            })
          })
        })
      })
    })
  },
  getPost(postid) {  // 获取具体某条帖子
    return wx.cloud.callFunction({
      name: 'getPosts',
      data: {
        postid: postid,
        skiplength: 0
      }
    })
  },
  getAllComments(postid, skiplength) { // 获取该帖子的全部评论信息
    return wx.cloud.callFunction({
      name: 'getComments',
      data: {
        postid: postid,
        skiplength: skiplength
      }
    })
  },
  onPullDownRefresh() {
    wx.showLoading({
      title: '加载中'
    })
    this.getPost(this.data.post._id).then(res => {
      var post = res.result.list[0]
      this.getAllComments(this.data.post._id, 0).then(res => {
        this.setData({
          post: post,
          comments: res.result.list
        })
        wx.hideLoading()
        wx.stopPullDownRefresh()
      })
    })
  },
  onReachBottom() {
    wx.showLoading({
      title: '加载中',
    })
    this.getAllComments(this.data.post._id, this.data.comments.length).then(res => {
      wx.hideLoading()
      if (res.result.list.length <= 0) {
        wx.showToast({
          icon: 'none',
          title: '没有更多评论啦',
        })
        return
      }
      this.setData({
        comments: this.data.comments.concat(res.result.list)
      })
    })
  },
})